$(function () {

	function _operation_field_formatter(cellValue , options , rowData){
		var mode = 'view';
		var buttons = [];
		var id = rowData['id'];
		buttons.push('<div class="grid-operation-button">')

		if(mode == 'view') {
			buttons.push(str_format('<a class="label label-info" target="_blank" href="{0}">下载</a>',rowData['videoUrl']));
		}
		if(mode == 'modify') {
			buttons.push(str_format('<a class="label label-info" onclick="T.vm.update(' + id + ',event)" href="javascript:;">修改</a>', rowData['id']));
		}
		buttons.push('</div>');
		return buttons.join("&nbsp;")
	}

	var queryUrl = baseURL + 'live/livevideo/list?';
	if(T.p("stream_id")){
		queryUrl = queryUrl + "&stream_id=" + T.p("stream_id")
	}
    $("#jqGrid").jqGrid({
        url: queryUrl,
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true },
			//{ label: '应用编号', name: 'appid', index: 'appid', width: 80 ,formatter : get_grid_column_formatter('live/livevideo/appid')},
			//{ label: '流名称', name: 'streamId', index: 'stream_id', width: 80 ,formatter : get_grid_column_formatter('live/livevideo/streamId')},
			{ label: '直播主题', name: 'title', index: 'title', width: 120 ,formatter : get_grid_column_formatter('live/livevideo/title')},
			{ label: '直播场次', name: 'liveRoundId', index: 'live_round_id', width: 120 ,formatter : get_grid_column_formatter('live/livevideo/live_round_id')},
			//{ label: '频道名称', name: 'channelId', index: 'channel_id', width: 80 ,formatter : get_grid_column_formatter('live/livevideo/channelId')},
			//{ label: '文件编号', name: 'fileId', index: 'file_id', width: 80 ,formatter : get_grid_column_formatter('live/livevideo/fileId')},
			//{ label: '文件格式', name: 'fileFormat', index: 'file_format', width: 80 ,formatter : get_grid_column_formatter('live/livevideo/fileFormat')},
			{ label: '录制开始时间', name: 'startTime', index: 'start_time', width: 80 ,formatter : timestampForMS},
			{ label: '录制结束时间', name: 'endTime', index: 'end_time', width: 80 ,formatter : timestampForMS},
			{ label: '录制时长(秒)', name: 'duration', index: 'duration', width: 80 ,formatter : get_grid_column_formatter('live/livevideo/duration')},
			{ label: '文件大小', name: 'fileSize', index: 'file_size', width: 80 ,formatter : set_filesize_formatter},
			//{ label: '推流参数', name: 'streamParam', index: 'stream_param', width: 80 ,formatter : get_grid_column_formatter('live/livevideo/streamParam')},
			//{ label: '录像url', name: 'videoUrl', index: 'video_url', width: 80 ,formatter : get_grid_column_formatter('live/livevideo/videoUrl')},
			//{ label: '原始日志', name: 'raw', index: 'raw', width: 80 ,formatter : get_grid_column_formatter('live/livevideo/raw')},
			{ label: '添加时间', name: 'adddate', index: 'adddate', width: 80 ,formatter : timestampForMS}
			,{ label: '操作', name: '_operation_field', index: '_operation_field',width : 40, formatter:_operation_field_formatter}
        ],
		viewrecords: true,
        height: 385,
        rowNum: 16,
		rowList : [16,30,50],
        rownumbers: false,
        rownumWidth: 25, 
        autowidth:true,
        multiselect: false,
        pager: "#jqGridPager",
		sortable:true,
		sortorder:"desc",
		sortname:'id',
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm  = T.vm = new Vue({
	el:'#webpage',
	data:{
		appearance : {
			showList: true,
			showControllBar : true,
			showSearchForm : false
		},
		q:{
			_keyword:''
		},
		title: null,
		liveVideo: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.appearance.showList = false;
			vm.title = "新增";
			vm.liveVideo = {};
		},
		update: function (targetId,event) {
			var id = targetId || getSelectedRow();
			if(id == null){
				return ;
			}
			vm.appearance.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.liveVideo.id == null ? "live/livevideo/save" : "live/livevideo/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.liveVideo),
			    success: function(r){
			    	if(r.code > 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "live/livevideo/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code > 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "live/livevideo/info/"+id, function(r){
                vm.liveVideo = r.liveVideo;
            });
		},
		reload: function (event) {
			vm.appearance.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
				postData:{'_keyword': vm.q._keyword},
                page:page
            }).trigger("reloadGrid");
		}
	}
});